package com.itmuch.core.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.itmuch.core.util.TimeUtil;
import com.itmuch.youran.security.SubjectUtil;

import tk.mybatis.mapper.common.Mapper;

public class BaseService<T extends Mapper<D>, D extends BaseEntity> {
	@Autowired
	private SubjectUtil subjectUtil;

	@Autowired
	protected T dao;

	public int insertSelective(D d) {
		d.setCreateTime(TimeUtil.now());

		Integer id = this.subjectUtil.getSubjectId();
		d.setCreateId(id);

		return this.dao.insertSelective(d);
	}

	public int updateByPrimaryKeySelective(D d) {
		d.setUpdateTime(TimeUtil.now());
		Integer id = this.subjectUtil.getSubjectId();
		d.setUpdateId(id);

		return this.dao.updateByPrimaryKeySelective(d);
	}

	public List<D> findAll() {
		return this.dao.selectAll();
	}

	public D selectByPrimaryKey(Integer id) {
		return this.dao.selectByPrimaryKey(id);
	}

	public void delete(Integer id) {
		this.dao.deleteByPrimaryKey(id);
	}

}
